/*  SSJumpMapSubViewRep.m
 *  MESLib
 * 
 * Copyright 2003-2009 Starline Software Ltd
 * 
 * This file is part of MESLib.
 * 
 * MESLib is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * MESLib is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public License
 * along with MESLib. If not, see <http://www.gnu.org/licenses/>.
 */
//  Created by Tobias Haughton on Fri Apr 18 2003.
//
//The SSJumpMapSubViewRep creates a representation of any NSView subclass to be displayed in
//the SSJumpMapView

#import "SSJumpMapSubViewRep.h"


@implementation SSJumpMapSubViewRep

- (id)initWithRepView:(NSView *)rep xRatio:(float)x yRatio:(float)y
{
   if (self = [super init]) {
      [self setRepresentedView:rep];
      [self updateWithXRatio:x yRatio:y];
   }
   return self;
}

- (void)draw
{
   if([representedView respondsToSelector:@selector(color)]) {
      //if the represented view responds to color we want to set that color and fill our frame
      if(![[representedView color] isEqualTo:[NSColor whiteColor]]) {
         [[representedView color] set];
         [NSBezierPath fillRect:[self frame]];
         return;
      }
   }

   //the representedView doesn't respond to color or its color is white so we will draw a black border around our frame instead
   [[NSColor blackColor] set];
   [NSBezierPath strokeRect:[self frame]];
}

- (void)updateWithXRatio:(float)x yRatio:(float)y
{
   xRatio = x;
   yRatio = y;
   [self setFrame:NSMakeRect((NSMinX([representedView frame]) * xRatio), (NSMinY([representedView frame]) * yRatio),
                             (NSWidth([representedView frame]) * xRatio), (NSHeight([representedView frame]) * yRatio))];
}

- (void)update
{
   [self setFrame:NSMakeRect((NSMinX([representedView frame]) * xRatio), (NSMinY([representedView frame]) * yRatio),
                             (NSWidth([representedView frame]) * xRatio), (NSHeight([representedView frame]) * yRatio))];
}

- (NSView *)representedView
{
   return representedView;
}

- (void)setRepresentedView:(NSView *)rep
{
   [representedView release];
   [rep retain];
   representedView = rep;

   //register so that we know when our representedView changes position
   [[NSNotificationCenter defaultCenter] addObserver:self
          selector:@selector(update)
              name:@"NSViewFrameDidChangeNotification"
            object:representedView];
}

- (NSRect)frame
{
   return frame;
}

- (void)setFrame:(NSRect)rect
{
   frame = rect;
}

- (void)dealloc
{
   [[NSNotificationCenter defaultCenter] removeObserver:self];
   [representedView release];
   [super dealloc];
}

@end
